//
//  AccumulativeMarker.swift
//  HuaYun
//
//  Created by enghou on 2018/8/22.
//  Copyright © 2018年 xyxorigation. All rights reserved.
//

import Charts

open class AccumulativeMarker: MarkerImage
{
    open var color: UIColor
    open var arrowSize = CGSize(width: 15, height: 11)
    open var font: UIFont
    open var textColor: UIColor
    open var insets: UIEdgeInsets
    open var minimumSize = CGSize()
    
    private var maxWidth: CGFloat = 0.0
    private var images: Array<UIImage>?
    var _label: String?
    var label: String? {
        set{
            _label = newValue
            
            _drawAttributes.removeAll()
            _drawAttributes[.font] = self.font
            _drawAttributes[.paragraphStyle] = _paragraphStyle
            _drawAttributes[.foregroundColor] = self.textColor
            
            _labelSize = label?.size(withAttributes: _drawAttributes) ?? CGSize.zero
            
            //        HorizontalBarMarker(color: UIColor(white: 0, alpha: 0.7), font: .systemFont(ofSize: 12), textColor: .white, insets: UIEdgeInsets(top: 10, left: 10, bottom: 10, right: 10))
            if let lab = label {
                _labelSize = lab.size(with: UIFont.systemFont(ofSize: 12), maxSize: CGSize.init(width: 150, height: 1000))
            }
            
            
            
            
            var size = CGSize()
            size.width = _labelSize.width + self.insets.left + self.insets.right
            size.height = _labelSize.height + self.insets.top + self.insets.bottom
            size.width = max(minimumSize.width, size.width)
            size.height = max(minimumSize.height, size.height)
            self.size = size
        }
        
        get{
            return _label
        }
    }
    var _labelSize: CGSize = CGSize()
    var _paragraphStyle: NSMutableParagraphStyle?
    var _drawAttributes = [NSAttributedString.Key : AnyObject]()
    
    public init(color: UIColor, font: UIFont, textColor: UIColor, insets: UIEdgeInsets)
    {
        self.color = color
        self.font = font
        self.textColor = textColor
        self.insets = insets
        
        _paragraphStyle = NSParagraphStyle.default.mutableCopy() as? NSMutableParagraphStyle
        _paragraphStyle?.alignment = .left
        super.init()
    }
    
    open override func offsetForDrawing(atPoint point: CGPoint) -> CGPoint
    {
        
        
        let size = CGSize(width: _labelSize.width + 20, height: _labelSize.height + 20)
        
        var p = CGPoint.zero
        
        if (UIScreen.main.bounds.size.width - point.x) < size.width / 2 {
            
            p.x = -size.width / 2
            
        }
        
        if (point.x - (size.width) / 2 < 0) {
            p.x = size.width / 2 - point.x
        }
        
        if point.y < 0 {
            p.y = -point.y
        }
        
        let height = UIScreen.main.bounds.size.width * 250 / 375
        
        if point.y + size.height > height - 20 {
            p.y = -point.y - size.height + height - 20
        }
        
        return p
        
    }
    
    open override func draw(context: CGContext, point: CGPoint)
    {
        guard let label = self.label else { return }
        
        let offset = self.offsetForDrawing(atPoint: point)
        
        var rect = CGRect(
            origin: CGPoint(
                x: point.x + offset.x,
                y: point.y + offset.y),
            size: size)
        
        rect.origin.x -= size.width / 2.0
        
        context.saveGState()
        
        context.setFillColor(color.cgColor)
        
        let path = CGMutablePath.init()
        
        path.addRoundedRect(in: rect, cornerWidth: 2, cornerHeight: 2)
        
        context.addPath(path)
        
        context.fillPath()
        
        UIGraphicsPushContext(context)
        
        label.draw(in: CGRect(x: rect.origin.x + insets.left, y: rect.origin.y + insets.top, width: _labelSize.width, height: _labelSize.height), withAttributes: _drawAttributes)
        
        UIGraphicsPopContext()
        
        context.restoreGState()
    }
    
    open override func refreshContent(entry: ChartDataEntry, highlight: Highlight)
    {
        
        let month = self.chartView!.data!.dataSets[highlight.dataSetIndex].entryIndex(entry: entry)
        
        var total: Double = 0.0
        
        let set = self.chartView!.data!.dataSets[0]
        
        for i in 0 ... month {
            
            total += set.entryForIndex(i)!.y
            
        }
        
        let monthTitle = "截至" + String(month + 1) + "月累计\n" + String(format: "%.2f", total) + "万元"
        
        self.label = monthTitle
        
        
    }
    
//    open func setLabel(_ newLabel: String)
//    {
//
//    }
}


